package com.op.util;

/**
 * Created by Genji on 2015/8/3.
 */

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.content.ContentResolver;
import android.net.Uri;
import android.os.Environment;


public class FileUtil {

    /**
     * 判断是否存在 SDcard
     *
     * @return boolean
     */
    public static boolean hasSdCard() {
        return Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED);
    }

    /**
     * 获取保存路径(绝对路径)
     *
     * @param path
     *            要保存的子路径
     * @return
     */
    public static String getSavePath(String path) {
        String savePath = Environment.getExternalStorageDirectory().toString()
                + path;
        File file = new File(savePath);
        if (!file.exists())
            file.mkdirs();
        return savePath;
    }

    /**
     * 保存文件到储存器
     *
     * @param uri
     *            传入文件的URI
     * @param subPath
     *            保存文件的子路径
     * @param fileName
     *            保存文件的名称
     * @param context
     *            当前Activity活动
     * @return File 保存后的File文件
     */
    public static File saveFile(Uri uri, String subPath, String fileName,Activity context) {
        ContentResolver cr = context.getContentResolver();
        File resultFile = null;
        try {
            InputStream in = cr.openInputStream(uri);
            BufferedInputStream bis = new BufferedInputStream(in);
            byte[] btye = new byte[1024 * 1024];
            int read = -1;
            resultFile = new File(getSavePath(subPath),fileName);
            FileOutputStream fos = new FileOutputStream(resultFile);
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            try {
                while ((read = bis.read(btye)) != -1) {
                    bos.write(btye, 0, read);
                }
                bos.close();
                bis.close();
                fos.close();
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        }
        return resultFile;
    }

    /**
     * 保存网络文件到储存器
     *
     * @param url
     *            传入文件的URL地址
     * @param subPath
     *            保存文件的子路径
     * @param fileName
     *            保存文件的名称
     * @param context
     *            当前Activity活动
     * @return File 保存后的File文件
     */
    public static File saveNetwordFile(URL url, String subPath, String fileName,Activity context) {
        File resultFile = null;
        URLConnection conntection = null;
        InputStream in = null;
        try {

            try {
                conntection = url.openConnection();
            } catch (IOException e1) {
                e1.printStackTrace();
                return null;
            }
            try {
                in = conntection.getInputStream();
            } catch (IOException e1) {
                e1.printStackTrace();
                return null;
            }
            BufferedInputStream bis = new BufferedInputStream(in);
            byte[] btye = new byte[1024 * 1024];
            int read = -1;
            resultFile = new File(getSavePath(subPath),fileName);
            FileOutputStream fos = new FileOutputStream(resultFile);
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            try {
                while ((read = bis.read(btye)) != -1) {
                    bos.write(btye, 0, read);
                }
                bos.close();
                bis.close();
                fos.close();
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        }
        return resultFile;
    }

    /**
     * 删除储存器文件
     * @param file
     * 		 傳入要刪除的文件
     * @return boolean
     * 		 是否刪除成功
     */
    public static boolean deleteFile(File file) {
        return file.delete();

    }

    /**
     *  获取储存器指定路径的所有文件
     * @param path
     * 			文件所有的绝对路径
     * @return File[]
     * 			路径下的所有文件
     */
    public static File[] getPathFiles(String path){
        File file = new File(path);
        File[] resultFiles = null;
        if(file.exists()){
            resultFiles = file.listFiles();
        }
        return resultFiles;
    }
    /**
     * 保存文件到储存器
     *
     * @param uri
     *            传入文件的URI
     * @param subPath
     *            保存文件的子路径
     * @param fileName
     *            保存文件的名称
     * @param context
     *            当前Activity活动
     * @return File 保存后的File文件
     */
    public static File saveFile(InputStream in, String subPath, String fileName,Activity context) {
        ContentResolver cr = context.getContentResolver();
        File resultFile = null;
        try {
            BufferedInputStream bis = new BufferedInputStream(in);
            byte[] btye = new byte[1024 * 1024];
            int read = -1;
            resultFile = new File(getSavePath(subPath),fileName);
            FileOutputStream fos = new FileOutputStream(resultFile);
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            try {
                while ((read = bis.read(btye)) != -1) {
                    bos.write(btye, 0, read);
                }
                bos.close();
                bis.close();
                fos.close();
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        }
        return resultFile;
    }

}

